from typing import Self

from pydantic import BaseModel, Field, model_validator


class AuthForm(BaseModel):
    username: str = Field(..., pattern=r"^[A-Za-z0-9_]{5,15}$")
    password: str = Field(..., pattern=r"^[A-Za-z0-9_]{8,20}$")


class RegisterForm(AuthForm):
    password2: str = Field(..., pattern=r"^[A-Za-z0-9_]{8,20}$")

    @model_validator(mode="after")
    def validate_password(self) -> Self:
        if self.password2 != self.password:
            raise ValueError("Passwords don't match")
        return self


class LoginForm(AuthForm):
    pass
